package cn.yougou.common;

import cn.yougou.pojo.*;
import cn.yougou.vo.YgCouponsSelAllVo;
import cn.yougou.vo.YgGetCouponListVo;
import cn.yougou.vo.YgResCommodityVo;
import cn.yougou.vo.YgUserInfoVo;
import cn.yougou.ygenum.CouponRulesEnum;
import cn.yougou.ygenum.GetRulesEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: DELL
 * @date: 2020/12/6 13:03
 * @description:
 */
@Component
public class VoDataUtil {
    @Autowired
    public EnumUtil enumUtil;
    @Autowired
    public AnalysisFileUtil analysisFileUtil;


    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd" );
    SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss" );
    /**
     * 返回一个时间类型
     * */
    public Date returnDate(String value){
        try {
            return dateFormat.parse(value);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 返回一个List
     * 存储内容为：用户 id列表
     * */
    public List returnUserIdList(List<YgUser> UserList){
        List list = new ArrayList();
        for(YgUser ygUser : UserList){
        list.add(ygUser.getId());
        }
        return list;
    }
    /**
     *
     * 拼接  YgUserInfoVo
     * */
    public List returnYgUserInfoVo(List<YgUser> ygUserList, List<YgUserBasicsinfo> ygUserBasicesInfoList){
        List<YgUserInfoVo> YgUserInfoVoList =new ArrayList<>();
        StringBuffer birthday=new StringBuffer();
        for(int i =0 ;i< ygUserList.size();i++){
            int success =0;
            int successNum=0;
            if(successNum <ygUserBasicesInfoList.size()){
                for(int o=0;o<ygUserBasicesInfoList.size();o++){
                    if(ygUserList.get(i).getId() == ygUserBasicesInfoList.get(o).getId()){
                        if(ygUserBasicesInfoList.get(o).getBirthday()!=null){
                            birthday.append(dateFormat.format(ygUserBasicesInfoList.get(o).getBirthday()));
                        }
                        YgUserInfoVoList.add(new YgUserInfoVo(ygUserList.get(i).getId(),
                                ygUserList.get(i).getUserid(),
                                ygUserList.get(i).getPhone(),
                                ygUserList.get(i).getPoint(),
                                ygUserList.get(i).getPreferentialcard(),
                                ygUserList.get(i).getMoney(),
                                ygUserBasicesInfoList.get(o).getLevel(),
                                ygUserBasicesInfoList.get(o).getSex(),
                                ygUserBasicesInfoList.get(o).getState(),
                                birthday.toString(),
                                ygUserBasicesInfoList.get(o).getEmail()));
                        success =1;
                        successNum++;
                        break;
                    }
                }
            }
            if(success ==0){
                YgUserInfoVoList.add(new YgUserInfoVo(ygUserList.get(i).getId(),
                        ygUserList.get(i).getUserid(),
                        ygUserList.get(i).getPhone(),
                        ygUserList.get(i).getPoint(),
                        ygUserList.get(i).getPreferentialcard(),
                        ygUserList.get(i).getMoney(),
                        null,
                        null,
                        null,
                        null,
                        null));
            }
            birthday.delete(0,birthday.length());
            }

    return YgUserInfoVoList;
    }
    /**
     * 组装 YgCouponsSelAllVo
     * 需要特殊处理的 参数
     * 优惠类型
     * 使用范围
     * 优惠卷类型
     * */
    public List returnYgCouponsSelAllVo(List<YgCoupons> list){
        boolean flag =true;
        //获得LinkcouponCategoryList  returnIntegerIDS(analysisFileUtil.getFileAssignLineList("108#107#108",0));
        Integer []  LinkcouponCategoryList=null;
       //获得LinkgoodsList
        Integer [] LinkgoodsList= null;
        //获得forbidScopeList
        Integer [] forbidScope=null;
        List newList = new ArrayList();
        StringBuffer stopDate = new StringBuffer();
        for(int i=0;i<list.size();i++){
            if((list.get(i).getStopdate() != null&&!list.get(i).getStopdate().equals(""))&&!list.get(i).getStopdate().equals("")){
                stopDate.append(list.get(i).getStopdate());
            }

        if(list.get(i).getTypeid()!=null &&!list.get(i).getTypeid().equals("*")){
            LinkcouponCategoryList=returnIntegerIDS(analysisFileUtil.getFileAssignLineList(list.get(i).getTypeid(),0));

        }else if(list.get(i).getTypeid()!=null &&list.get(i).getTypeid().equals("*")){
            LinkcouponCategoryList =new Integer []{-1};//等于-1 就等于全部
        }else{
            LinkcouponCategoryList=null;
        }

        if((list.get(i).getCommodityid()!=null&&!list.get(i).getCommodityid().equals("")) &&!list.get(i).getCommodityid().equals("*")){
            LinkgoodsList=returnIntegerIDS(analysisFileUtil.getFileAssignLineList(list.get(i).getCommodityid(),0));

        }else if(list.get(i).getCommodityid()!=null&&list.get(i).getCommodityid().equals("*")){
            LinkgoodsList=new Integer []{-1};//等于-1 就等于全部
        }else{
            LinkgoodsList=null;
        }

        if((list.get(i).getForbidScope()!=null&&!list.get(i).getForbidScope().equals("")) &&!list.get(i).getForbidScope().equals("*")){
            forbidScope=returnIntegerIDS(analysisFileUtil.getFileAssignLineList(list.get(i).getForbidScope(),0));

            }else if(list.get(i).getForbidScope()!=null&&list.get(i).getForbidScope().equals("*")){
            forbidScope=new Integer []{-1};//等于-1 就等于全部
            }else{
            forbidScope=null;
            }
        if(LinkcouponCategoryList !=null&&LinkgoodsList!=null){
            if(LinkcouponCategoryList[0]==-1&&LinkgoodsList[0]==-1){
                flag =false;
            }
        }

            newList.add(new YgCouponsSelAllVo(
                    list.get(i).getId(),
                    list.get(i).getCoupontitile(),
                    list.get(i).getCouponrulestitle(),
                    enumUtil.returnCouponRulesEnumValue(list.get(i).getCouponrules()),
                    list.get(i).getCouponrulesjudge(),
                    list.get(i).getCouponmoney(),
                    list.get(i).getRepetitionget(),
                    flag?list.get(i).getGetrules():"*",
                    enumUtil.returnGetRulesEnumValue(list.get(i).getGetrules()),
                    list.get(i).getCouponscount(),
                    list.get(i).getCouponsTime(),
                    stopDate.toString(),
                    list.get(i).getState(),
                    LinkcouponCategoryList,
                    LinkgoodsList,
                    forbidScope
            ));
            stopDate.delete(0,stopDate.length());
            flag=true;
            LinkcouponCategoryList=null;
            LinkgoodsList= null;
            forbidScope=null;
       }
        return newList;
    }

/**
 * 遍历 List<Integer> 返回 Integer []
 * */
     public  Integer [] returnIntegerIDS(List<String> list){
         Integer [] data= null;
         if(list!=null){
              data= new Integer [list.size()];
             for(int i =0;i<list.size();i++){
                 try {
                     data[i]=Integer.valueOf(list.get(i)) ;
                 }catch (Exception e){
                     continue;
                 }

             }
         }
         return data;
     }
     /**
      * 返回一个 YgGetCouponListVo list
      * */
     public List YgGetCouponListVo(List<YgCoupons> ocnformToRulesList){
         if(ocnformToRulesList != null){
             List<YgGetCouponListVo> list = new ArrayList<>();
             for(int i =0;i<ocnformToRulesList.size();i++){
                 list.add(new YgGetCouponListVo(ocnformToRulesList.get(i).getId(),ocnformToRulesList.get(i).getCoupontitile(),ocnformToRulesList.get(i).getCouponrulestitle(),ocnformToRulesList.get(i).getCouponmoney(),ocnformToRulesList.get(i).getCouponrulesjudge(),DateUtil8.getyyyyMMdd_ENString(ocnformToRulesList.get(i).getStopdate()),ocnformToRulesList.get(i).getIsGet()));
             }
             return list;
         }
       return null;
     }
    /**
     * 返回一个 YgGetCouponListVo list
     * */
    public List YgGetCouponListVoUserCoupons(List<YgUserCoupons> ocnformToRulesList,Integer isGet){
        if(ocnformToRulesList != null){
            List<YgGetCouponListVo> list = new ArrayList<>();
            for(int i =0;i<ocnformToRulesList.size();i++){
                list.add(new YgGetCouponListVo(ocnformToRulesList.get(i).getId(),ocnformToRulesList.get(i).getGetrulestitle(),ocnformToRulesList.get(i).getCouponrulestitle(),ocnformToRulesList.get(i).getCouponmoney(),ocnformToRulesList.get(i).getCouponrulesjudge(),(ocnformToRulesList.get(i).getEnddate()==null?"永久":ocnformToRulesList.get(i).getEnddate()),isGet));//能用就1 不能用就0
            }
            return list;
        }
        return null;
    }
    public  YgResCommodityVo[] YgCartTransformYgResCommodityVo(YgCart[] ygCarts){

        YgResCommodityVo[] ygResCommodityVos =null;
        if(ygCarts!=null){
          ygResCommodityVos = new YgResCommodityVo [ygCarts.length];
            for(int i =0;i < ygCarts.length; i++){
                ygResCommodityVos[i]=new YgResCommodityVo(
                        ygCarts[i].getCommodityid(),
                        ygCarts[i].getProductid(),
                        ygCarts[i].getCommodityname(),
                        ygCarts[i].getNumber(),
                        ygCarts[i].getPrice(),
                        ygCarts[i].getSpecifications()
                );
            }
        }
        return ygResCommodityVos;
    }
}
